import request from '@/utils/request'
import type { ApiResponse, PageResponse } from '@/types/api'
import { ElMessage, ElMessageBox } from 'element-plus'
import { validateExcelFile } from '@/utils/request'


const base = '/test/sysGenTest'

/**
 * SysGenTest API（单表模式）
 */
export const sysGenTestApi = {

  /**
   * 分页查询SysGenTest
   */
  listSysGenTest: async (params: any): Promise<ApiResponse<PageResponse<any>>> => {
    return request.get(`${base}`, { params })
  },

  /**
   * 查询SysGenTest详情
   */
  getSysGenTest: async (id: string | number): Promise<ApiResponse<any>> => {
    return request.get(`${base}/${id}`)
  },

  /**
   * 创建SysGenTest
   */
  createSysGenTest: async (data: any): Promise<ApiResponse<any>> => {
    return request.post(base, data)
  },

  /**
   * 更新SysGenTest
   */
  updateSysGenTest: async (id: string | number, data: any): Promise<ApiResponse<any>> => {
    return request.put(`${base}/${id}`, data)
  },

  /**
   * 删除SysGenTest
   */
  deleteSysGenTest: async (id: string | number): Promise<ApiResponse<void>> => {
    return request.delete(`${base}/${id}`)
  },

  /**
   * 批量删除SysGenTest
   */
  batchDeleteSysGenTest: async (ids: (string | number)[]): Promise<ApiResponse<void>> => {
    return request.delete(`${base}/batch`, { data: ids })
  },

  /**
   * 导出SysGenTest
   */
  exportSysGenTest: async (params: any): Promise<void> => {
    return request.download({
      url: `${base}/export`,
      method: 'post',
      data: params,
      filename: `SysGenTest列表_${new Date().toISOString().slice(0, 10)}.xlsx`
    })
  },

  /**
   * 导入SysGenTest
   */
  importSysGenTest: async (file: File, updateSupport: boolean = false): Promise<ApiResponse<any>> => {
    const formData = new FormData()
    formData.append('file', file)
    formData.append('updateSupport', String(updateSupport))
    return request.upload({
      url: `${base}/import`,
      data: formData
    })
  },

  /**
   * 下载SysGenTest导入模板
   */
  downloadSysGenTestTemplate: async (): Promise<void> => {
    return request.download({
      url: `${base}/template`,
      filename: `SysGenTest导入模板.xlsx`
    })
  }

}


/**
 * 导出SysGenTest（带UI交互）
 */
export const exportSysGenTestWithUI = async (params: any) => {
  try {
    await sysGenTestApi.exportSysGenTest(params)
    ElMessage.success('导出成功')
  } catch (error) {
    console.error('导出失败:', error)
    ElMessage.error('导出失败')
  }
}

/**
 * 导入SysGenTest（带UI交互）
 */
export const importSysGenTestWithUI = async (
  file: File,
  updateSupport: boolean,
  callback?: () => void
) => {
  // 验证文件
  if (!validateExcelFile(file)) {
    return
  }

  try {
    const response = await sysGenTestApi.importSysGenTest(file, updateSupport)
    const result = response.data
    
    // 显示导入结果
    if (result.failureCount > 0) {
      ElMessageBox.alert(
        `导入完成！成功: ${result.successCount}, 失败: ${result.failureCount}`,
        '导入结果',
        {
          dangerouslyUseHTMLString: true,
          type: 'warning'
        }
      )
    } else {
      ElMessage.success(`导入成功！共导入 ${result.successCount} 条数据`)
    }
    
    // 执行回调
    if (callback) {
      callback()
    }
  } catch (error) {
    console.error('导入失败:', error)
    ElMessage.error('导入失败')
  }
}

/**
 * 下载SysGenTest导入模板（带UI交互）
 */
export const downloadSysGenTestTemplateWithUI = async () => {
  try {
    await sysGenTestApi.downloadSysGenTestTemplate()
    ElMessage.success('模板下载成功')
  } catch (error) {
    console.error('下载模板失败:', error)
    ElMessage.error('下载模板失败')
  }
}



export default sysGenTestApi


